package com.ylink.mqtt.test;

import java.io.IOException;

import org.junit.Test;

public class PahoMqttClientPublish {

	private final String MQTT_HOST =  "m2m.eclipse.org"; //"tcp://197.3.11.16"

	@Test
	public void test() {
		PahoMqttClient client = new PahoMqttClient();
		client.setHost(MQTT_HOST);
		client.setPort(1883);
		client.setClientId("TestCase2");

		client.connect(null, null, 30 * 1000, null, false, null);
		// client.subscribe("msbank/9472bf0296d9baa2", 2);

		MessagingMessage message = new MessagingMessage();
		message.setDestinationName("yxbank/mmp/procpush/clientId/99376D81-C865-4DC9-9113-9788D55ADB4C");
		message.setDestinationType("2");
		message.setDuplicate(true);
		message.setMessage("这是一个mqtt测试，如果你的接收端收到该消息，说明mqtt测试成功，祝你好运。。。");
		message.setQos(2);
		message.setRetained(false);
		message.setPayload(message.getMessage().getBytes());
		client.publish(message);

		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	public void push(String host, MessagingMessage msg){
		PahoMqttClient client = new PahoMqttClient();
		client.setHost(MQTT_HOST);
		client.setPort(1883);
		client.setClientId("TestCase2");

		client.connect(null, null, 30 * 1000, null, false, null);
		client.publish(msg);
	}

}
